/**
 * file: SimpleTestJob.java
 */

package pcsd.test;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.RemoteException;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import pcsd.server.JobQueueServer;
import pcsd.task.Pi;

/**
 * Simple test class.
 *
 * This class starts the server and gives it a bunch of tasks that need to be
 * processed. 
 *
 * @author Tim van Deurzen
 */
public class SimpleTestJob {

    public static int digits = 10;
    /**
     * Program entry point ;-).
     *
     * Parse the arguments, setup the server, create the tasks, collect the
     * information from the server if necessary.
     *
     * @param args The commandline arguments.
     */
	public static void main(String[] args) {
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new SecurityManager());
		}
		try {
		    parseArgs(args);

		    // Find the server
			String name = "server";
			Registry registry = LocateRegistry.getRegistry();
			JobQueueServer serv = (JobQueueServer) registry.lookup(name);

			Pi task = new Pi(digits);
			

			// Start sending tasks

				testThroughput(serv, task);

			
			System.out.println("Done enqueuing task.");
		}
		catch (Exception e) {
			System.err.println("Test exception:");
			e.printStackTrace();
		}
	}
	
	private static void testThroughput(JobQueueServer serv, Pi task){

		double whole = 0;

try{
		for(int j=0; j<5; j++){

			long startTime = System.currentTimeMillis();
		for (int i = 0; i < 100; i++) {


			serv.enqueueTask(task);


		}
			long stopTime = System.currentTimeMillis();
						double elapsedTime = (stopTime - startTime);

                whole += 100000 / elapsedTime;
                
                System.out.println(elapsedTime);
				
		Thread.sleep(2000);
	        }
	        
	        System.out.println("Average whole Time for enqueueTask: " + (whole / 5) + "tasks/s");
	        }
	        catch(Exception e){
	        e.printStackTrace();
	        }
	}

    /**
     * Parse command-line arguments.
     *
     * Instead of mixing this code into the code in main, we use this function
     * to allow easy extension of the number of arguments.
     */
    public static void parseArgs(String[] args) {
        int argc = args.length,
            i    = 0;

        if (argc == 0) {
            usage();
        }

        for (; i < argc; i++) {
            if (args[i].equals("-n")) {
                try {
                    digits = Integer.parseInt(args[++i]);
                } catch(NumberFormatException e) {
                    System.out.println(
                            "Number of digits must be a valid integer!"
                            );
                    usage();
                }
            } 
        }
    }

    /**
     * A simple usage method.
     *
     * Print some information for the users.
     */
    public static void usage() {
        System.out.println("Usage: java pcsd.test.SimpleTestJob [options]");
        System.out.println("\t -n <int> \t The number of digits of Pi to calculate.");
    }
}
